package com.hymjweb.demo.hymj.master_bill_manage.auto.controller;

import com.alibaba.fastjson.JSONObject;
import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.DateUtil;
import com.hymjweb.demo.hymj.master_bill_manage.auto.bean.BMasterBillAutoBean;
import com.hymjweb.demo.hymj.master_bill_manage.auto.bean.BMasterBillAutoPojo;
import com.hymjweb.demo.hymj.master_bill_manage.auto.service.BMasterBillAutoService;
import com.hymjweb.demo.hymj.master_bill_manage.flight.service.BMasterBillFlightService;
import com.hymjweb.demo.hymj.master_bill_manage.load.bean.BNativeBillLoadPojo;
import com.hymjweb.demo.hymj.master_bill_manage.load.service.BNativeBillLoadService;
import com.hymjweb.demo.hymj.master_bill_manage.other_charges.bean.BMasterBillOtherChargePojo;
import com.hymjweb.demo.hymj.master_bill_manage.other_charges.service.BMasterBillOtherChargeService;
import com.hymjweb.demo.hymj.native_bill_manage.bean.BNativeBillPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pages/master_bill_manage/auto")
public class BMasterBillAutoController extends BaseController {

    @Autowired
    private BMasterBillAutoService bMasterBillAutoService;

    @Autowired
    private BNativeBillLoadService bNativeBillLoadService;

    @Autowired
    private BMasterBillOtherChargeService bMasterBillOtherChargeService;


    /**
     * 初始化进入汽运主单添加页面
     *
     * @return 汽运主单添加页面地址
     */
    @RequestMapping("/createAutoMasterBill")
    public String createAutoMasterBill() {

        // 设置汽运主单ID
        setRequestAttribute("id", this.getSysStrId());

        setRequestAttribute("openMasterBillDate", DateUtil.dateToStr_yyyy_MM_dd(DateUtil.getSysDate()));
        setRequestAttribute("openMasterBillDepartmentId", getLoginUserDepartment().getId());
        setRequestAttribute("openMasterBillUserId", getLoginUserId());

        setRequestAttribute("opType", SysConstant.OPER_ADD);

        return "pages/master_bill_manage/auto/createAutoMasterBill";
    }


    /**
     * 查询主单列表
     *
     * @return 主单列表页面地址
     */
    @RequestMapping("/autoMasterBillList")
    public String nativeBillList() {
        return "pages/master_bill_manage/auto/autoMasterBillList";
    }


    /**
     * 根据查询条件查询主单列表
     *
     * @param param 查询参数
     * @return 返回查询列表对象
     * @throws Exception 抛出异常
     */
    @RequestMapping("/queryAutoMasterBill")
    @ResponseBody
    public Object queryAutoMasterBill(@RequestParam(required = true) Map param) {
        return bMasterBillAutoService.getPageList(param);
    }


    /**
     * 根据主单号获取主单配置的分单原始对象信息
     *
     * @param masterBillId
     * @return
     * @throws Exception
     */
    @RequestMapping("/queryBNativeBillPojoListByMasterBillId")
    @ResponseBody
    public Object queryBNativeBillPojoListByMasterBillId(String masterBillId) throws Exception {
        if (DataTypeUtil.isEmptyStr(masterBillId)) {
            throwException("空运主单查询包含原始配载分单参数错误");
        }

        List<BNativeBillPojo> bNativeBillPojoList = bMasterBillAutoService.queryBNativeBillPojoListByMasterBillId(masterBillId);

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setData(bNativeBillPojoList);
        return baseResultMap;
    }


    /**
     * 根据查询条件查询主单包含的分单
     *
     * @param param
     * @return
     * @throws Exception
     */
    @RequestMapping("/queryNativeBillList")
    @ResponseBody
    public Object queryNativeBillList(@RequestParam(required = true) Map param) throws Exception {
        if (param == null || param.isEmpty()) {
            throwException("汽运主单查询包含分单参数错误");
        }
        return bMasterBillAutoService.queryNativeBillList(param);
    }

    @RequestMapping("/queryMasterBillAutoPojoById")
    @ResponseBody
    public Object queryMasterBillAutoPojoById(String masterBillId) throws Exception {
        if (DataTypeUtil.isEmptyStr(masterBillId)) {
            throwException("根据主单ID加载主单对象失败!");
        }

        BMasterBillAutoPojo autoPojo = bMasterBillAutoService.getBMasterBillAutoPojoById(masterBillId);
        if (autoPojo == null) {
            throwException("根据主单ID加载主单对象失败，主单对象可能被删除!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setData(autoPojo);

        return baseResultMap;
    }

    /**
     * 新添加主单
     *
     * @param jsonMasterBillPojo 新加主单对象
     * @return 添加结果对象
     */
    @RequestMapping("/addMasterAutoBill")
    @ResponseBody
    public Object addMasterAutoBill(@RequestBody(required = true) JSONObject jsonMasterBillPojo) throws Exception {

        if (jsonMasterBillPojo == null) {
            throwException("添加汽运主单参数错误");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("保存汽运主单成功!");

        // json 对象转 Java 对象
        BMasterBillAutoBean bMasterBillFlightBean = bMasterBillAutoService.convertJSONObjToJavaObj(jsonMasterBillPojo);

        // 写入主单信息与主单配置的分单信息
        int effectRows = bMasterBillAutoService.addMasterAutoBill(bMasterBillFlightBean);

        if (effectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("添加汽运主单失败，请重试!");
        }

        return baseResultMap;
    }


    @RequestMapping("/showUpdateMasterBill")
    public String showUpdateBill(String opType, String id) throws Exception {

        if (DataTypeUtil.isEmptyStr(opType) || DataTypeUtil.isEmptyStr(id)) {
            throwException("获取汽运主单对象参数错误");
        }

        // 1.获取汽运主单对象
        BMasterBillAutoPojo pojo = bMasterBillAutoService.getBMasterBillAutoPojoById(id);
        if (pojo==null) {
            throwException("获取汽运主单对象失败!");
        }

        // 2 获取汽运主单位包含的分单列表对象
        Map<String, Object> param1 = new HashMap<>();
        param1.put("masterBillId", pojo.getId());
        List<BNativeBillLoadPojo> bNativeBillLoadPojoList = bMasterBillAutoService.queryNativeBillListNoPage(param1);

        // 根据获取到的加载对象，再去取到分单对象，根据获取到的分前单对象，再更新其配置值，再进行返回前端
        List<BNativeBillPojo> loadBNativeBillPojoList = bNativeBillLoadService.getLoadBNativeBillPojoByLoadList(bNativeBillLoadPojoList);

        // 3.获取汽运主单位包含的其他费用列表对象
        List<BMasterBillOtherChargePojo> bMasterBillOtherChargePojos = bMasterBillOtherChargeService.queryListByMasterBillId(pojo.getId());

        // 承运单位 前端根据承运方式 前端处理
        // 提货点 前端处理

        Object pojo1 = objectToJSONObject(pojo);
        // 主单对象
        setRequestAttribute("pojo", pojo1);
        // 主单包含的已配载的分单对象
        setRequestAttribute("loadBNativeBillPojoList", objectToJSONObject(loadBNativeBillPojoList));
        // 主单包含的其也费用对象
        setRequestAttribute("pojoOtherChargeList", objectToJSONObject(bMasterBillOtherChargePojos));

        // 设定操作类型为update
        setRequestAttribute("opType", opType);

        return "pages/master_bill_manage/auto/createAutoMasterBill";
    }

    /**
     * 更新主单
     *
     * @param jsonMasterBillPojo 需要更新的主单对象
     * @return 更新主单结果对象
     */
    @RequestMapping("/updateMasterAutoBill")
    @ResponseBody
    public Object updateMasterAutoBill(@RequestBody(required = true) JSONObject jsonMasterBillPojo) throws Exception {

        if (jsonMasterBillPojo == null) {
            throwException("更新汽运主单参数错误!");
        }


        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("更新汽运主单成功!");

        // json 对象转 Java 对象
        BMasterBillAutoBean bMasterBillAutoBean = bMasterBillAutoService.convertJSONObjToJavaObj(jsonMasterBillPojo);

        if (bMasterBillAutoBean == null) {
            throwException("更新汽运主单对象转换失败!");
        }

        // 写入主单信息与主单配置的分单信息
        int effectRows = bMasterBillAutoService.updateMasterAutoBill(bMasterBillAutoBean);

        if (effectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("更新汽运主单失败!");
        }

        return baseResultMap;
    }


    /**
     * 主单对账状态修改
     *
     * @param param
     * @return
     * @throws Exception
     */
    @RequestMapping("/changeAccountCheckStatus")
    @ResponseBody
    public Object changeAccountCheckStatus(@RequestBody(required = true) Map param) throws Exception {
        if (param == null || param.isEmpty()) {
            throwException("汽运主单对账参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("汽运主单对账成功!");

        // {billIds:canAccountCheckIds,accountCheckStatusId:accountStatus}
        int affectRows = bMasterBillAutoService.changeAccountCheckStatus(param);

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("汽运主单对账失败!");
        }
        return baseResultMap;
    }


    /**
     * 主单删除，参数形式为 id1,id2,id3
     *
     * @param param
     * @return
     */
    @RequestMapping("/deleteMasterBillByMasterBillIds")
    @ResponseBody
    public Object deleteMasterBillByMasterBillIds(@RequestBody(required = true) Map param) throws Exception {

        if (param == null || param.isEmpty()) {
            throwException("汽运主单删除参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("主单删除成功!");

        int affectRows = bMasterBillAutoService.deleteMasterBillByMasterBillIds(param);

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("主单删除失败!");
        }
        return baseResultMap;
    }

}
